﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using Lib.Utils;

namespace MS.INV
{
    public partial class MHome : Lib.Bases.Forms.BaseForm
    {
        public MHome()
        {
            InitializeComponent();

            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.TopLevel = false;
            this.NotEsc = true;
        }

        private void MHome_Load(object sender, EventArgs e)
        {
            OnLoad(false);
            dtNgay.EditValue = DateTime.Now;

            lblRef.LinkColor = SkinForeColor();
        }

        private void OnLoad(bool OnlyBusy)
        {
            using (SysLst.L_Ban b = new SysLst.L_Ban())
            {
                if (OnlyBusy)
                    mainView.Tables = b.GetAllBusyTable();
                else
                    mainView.Tables = b.GetAll();
            }
            using (SysLst.L_ViTriBan vt = new SysLst.L_ViTriBan())
                mainView.Position = vt.LoadData();
            using (SysLst.L_NhomHang lh = new SysLst.L_NhomHang())
                menu1.Items = lh.LoadData();
        }

        private void menu1_ItemClicked(object sender, EventArgs e)
        {
            DataRow row = (DataRow)sender;
            using (SysObj.L_HangHoa hh = new SysObj.L_HangHoa())
            {
                DataTable Detail = hh.GetMenuItemByGroupId(Cvert.ToInt32(row["IdNhom"], 0));
                menu1.SetDetail(Detail, "MaHang,TenHang,HinhAnh", "Mã,Tên,Hình");
            }
        }

        private void mainView_DataExchange(LibExtend.Events.Sender sender)
        {
            sender.Error = "";
            try
            {
                if (sender.ObjectName == LibExtend.Events.ObjectName.Catalog &&
                        sender.ActionType == LibExtend.Events.ActionType.Get)
                {
                    using (SysObj.L_HangHoa hh = new SysObj.L_HangHoa())
                        sender.ObjectValue = hh.ListMenu();
                }
                else if(sender.ObjectName == LibExtend.Events.ObjectName.Units &&
                        sender.ActionType == LibExtend.Events.ActionType.Get)
                {
                    using(SysLst.L_DVT dvt=new SysLst.L_DVT())
                        sender.ObjectValue=dvt.LoadData();
                }
                else if (sender.ObjectName == LibExtend.Events.ObjectName.Update &&
                        sender.ActionType == LibExtend.Events.ActionType.Clicked)
                {
                    using (SysObj.Bh_BanHang bh = new SysObj.Bh_BanHang())
                    {
                        bh.IdBanHang = sender.Item.IdBanHang;
                        bh.Ngay = sender.Item.Ngay;
                        bh.IdBan = sender.Item.IdBan;
                        bh.ThanhTien = sender.Item.ThanhTien;
                        bh.PT_CK = (int)sender.Item.PT_CK;
                        bh.Tien_CK = sender.Item.Tien_CK;
                        bh.KhachTra = sender.Item.KhachTra;
                        bh.TongTien = sender.Item.TongTien;
                        bh.GhiChu = sender.Item.GhiChu;
                        bh.D_HH = sender.Item.DataSource;
                        bh.OnUpdate();

                        sender.Item.IdBanHang = bh.IdBanHang;
                        dtNgay_EditValueChanged(null, new EventArgs());
                    }
                }
                else if (sender.ObjectName == LibExtend.Events.ObjectName.Delete &&
                    sender.ActionType == LibExtend.Events.ActionType.Clicked)
                {
                    if (MessageBox.Show("Bạn có chắc chắn xóa bỏ dữ liệu của bàn này hay không ?", "Xác nhận.",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                    {
                        sender.Cancel = true;
                        return;
                    }

                    using (SysObj.Bh_BanHang bh = new SysObj.Bh_BanHang())
                    {
                        bh.IdBanHang = sender.Item.IdBanHang;
                        bh.OnDelete();

                        sender.Item.IdBanHang = 0;
                        sender.Item.iState = LibExtend.Table.State.Empty;
                        mainView.lbRef_Click(null, new EventArgs());
                    }
                    dtNgay_EditValueChanged(null, new EventArgs());
                }
                //Sự kiện chọn xem thông tin chi tiết bàn đã gộp
                else if (sender.ObjectName == LibExtend.Events.ObjectName.MergeItem &&
                    sender.ActionType == LibExtend.Events.ActionType.Clicked)
                {
                    AssignItemFromSaleID(sender);
                }
                else if (sender.ObjectName == LibExtend.Events.ObjectName.None &&
                    sender.ActionType == LibExtend.Events.ActionType.UnGroup)
                {
                    if (MessageBox.Show("Bạn có chắc chắn muôn bỏ bàn này ra khỏi nhóm đã gộp hay không ?", "Xác nhận.",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                    {
                        sender.Cancel = true;
                        return;
                    }
                    using (SysObj.Bh_BanHang_GopBan gop = new SysObj.Bh_BanHang_GopBan())
                    {
                        gop.UnMerge(sender.Item.IdBanHang);
                    }
                    AssignItemFromSaleID(sender);
                }
                else if (sender.ObjectName == LibExtend.Events.ObjectName.SummaryMerge &&
                    sender.ActionType == LibExtend.Events.ActionType.Clicked)
                {
                    using (SysObj.Bh_BanHang bh = new SysObj.Bh_BanHang())
                    {
                        sender.Item.DataSource = bh.GetSummary(sender.Item.IdBanHang);
                        sender.Item.Ngay = bh.Ngay;
                        sender.Item.IdBan = bh.IdBan;
                        sender.Item.ThanhTien = bh.ThanhTien;
                        sender.Item.PT_CK = bh.PT_CK;
                        sender.Item.Tien_CK = bh.Tien_CK;
                        sender.Item.TongTien = bh.TongTien;
                        sender.Item.KhachTra = bh.KhachTra;
                        sender.Item.GhiChu = bh.GhiChu;
                        using (SysObj.Bh_BanHang_GopBan mer = new SysObj.Bh_BanHang_GopBan())
                        {
                            sender.Item.DataMerge = mer.LoadMergeItemByID(bh.IdBanHang);
                        }
                    }
                }
                else if (sender.ObjectName == LibExtend.Events.ObjectName.UpdateSummary &&
                    sender.ActionType == LibExtend.Events.ActionType.Clicked)
                {
                    //MessageBox.Show("Update summary");
                    using (SysObj.Bh_BanHang bh = new SysObj.Bh_BanHang())
                    {
                        bh.UpdateSummary(sender.Item.IdBanHang);
                    }
                }
                else if (sender.ObjectName == LibExtend.Events.ObjectName.Print &&
                    sender.ActionType == LibExtend.Events.ActionType.Clicked)
                {
                    onPrint("Bill.repx", sender.Item.IdBanHang);
                }
                else if (sender.ObjectName == LibExtend.Events.ObjectName.Print &&
                    sender.ActionType == LibExtend.Events.ActionType.Changed)
                {
                    Lib.Bases.Configs.LocalXml.PrinterName = LibExtend.Config.PrinterName;
                }
                else if (sender.ObjectName == LibExtend.Events.ObjectName.Price && sender.ActionType == LibExtend.Events.ActionType.Get)
                {
                    int v_id = Cvert.ToInt32(sender.ObjectValue, 0);
                    sender.ObjectValue = 0;

                    using (SysObj.Price_System pri = new SysObj.Price_System(sender.Item.Ngay, sender.Item.IdBan, v_id))
                    {
                        sender.ObjectValue = pri.GetSalePrice();
                    }
                }
                else if (sender.ObjectName == LibExtend.Events.ObjectName.History && sender.ActionType == LibExtend.Events.ActionType.Get)
                {
                    //Get history by table id and day
                    using (SysObj.Bh_BanHang bh = new SysObj.Bh_BanHang())
                    {
                        sender.ObjectValue = bh.GetTableHistory(sender.Item.IdBan, sender.Item.Ngay);
                    }
                }
                else if (sender.ObjectName == LibExtend.Events.ObjectName.Review && sender.ActionType == LibExtend.Events.ActionType.Get)
                {
                    //Review table by sale id
                    LibExtend.Events.Sender _sender = sender;
                    _sender.Item.IdBanHang = Cvert.ToInt32(sender.ObjectValue, 0);
                    AssignItemFromSaleID(_sender);
                    sender = _sender;
                }
                else if (sender.ObjectName == LibExtend.Events.ObjectName.Current && sender.ActionType == LibExtend.Events.ActionType.Get)
                {
                    //Get current table info
                    mainView_ItemLicked(sender.Item);
                }
                else if (sender.ObjectName == LibExtend.Events.ObjectName.PrintMix && sender.ActionType == LibExtend.Events.ActionType.Clicked) 
                {
                    onPrint("Mix.repx", sender.Item.IdBanHang);
                }
                    //Get datasource when group or merge table with option select detail menu
                else if (sender.ObjectName == LibExtend.Events.ObjectName.DataSource && sender.ActionType == LibExtend.Events.ActionType.Get)
                {
                    using (SysObj.Bh_BanHang bh = new SysObj.Bh_BanHang())
                    {
                        LibExtend.Table tbl = new LibExtend.Table();

                        bh.IdBanHang = sender.Item.IdBanHang;
                        tbl.DataSource = bh.GetDetail();
                        using (SysObj.Bh_BanHang_GopBan mer = new SysObj.Bh_BanHang_GopBan())
                        {
                            tbl.DataMerge = mer.LoadMergeItemByID(bh.IdBanHang);
                        }
                        sender.ObjectValue = tbl;
                    }
                }
                    //move or merge accept changed
                else if (sender.ObjectName == LibExtend.Events.ObjectName.mmo && sender.ActionType == LibExtend.Events.ActionType.Changed)
                {
                    DoMerge(sender);
                }
            }
            catch (Exception ex)
            {
                sender.Cancel = true;
                sender.Error = ex.Message;
                Lib.Bases.SysError.RaiseError(ex, this.Name, "Data exchange");
            }
        }

        private void DoMerge(LibExtend.Events.Sender sender)
        {
            if (sender.mmo.MoveType == 1)//Move or split
            {
                if (sender.mmo.MenuOption == 1)//All menu
                {
                    using (SysObj.Bh_BanHang bh = new SysObj.Bh_BanHang())
                    {
                        bh.UpdateNewTable(sender.Item.IdBanHang, sender.ToItem.IdBan);
                        //refresh state

                        sender.ToItem.IdBanHang = sender.Item.IdBanHang;
                        sender.Item.IdBanHang = 0;
                        sender.ToItem.iState = sender.Item.iState;
                        sender.Item.iState = LibExtend.Table.State.Empty;

                        mainView.lbRef_Click(null, new EventArgs());
                    }
                }
                else if (sender.mmo.MenuOption == 2)//Menu selection
                {
                    if (!object.Equals(sender.mmo.DataSource, null) && sender.mmo.DataSource.Rows.Count > 0)
                    {
                        int new_id = 0;
                        using (SysObj.Bh_BanHang_GopBan move = new SysObj.Bh_BanHang_GopBan())
                        {
                            new_id = move.MoveOrSplit(sender.Item.IdBanHang, sender.Item.IdBan, sender.ToItem.IdBan, sender.mmo.DataSource);
                        }
                        sender.ToItem.IdBanHang = new_id;
                        sender.ToItem.iState = LibExtend.Table.State.Busy;
                        mainView.lbRef_Click(null, new EventArgs());
                    }
                }
            }
            else if (sender.mmo.MoveType == 2)//Merge
            {
                if (sender.mmo.MergeOption == 1)
                {
                    using (SysObj.Bh_BanHang_GopBan gop = new SysObj.Bh_BanHang_GopBan())
                    {
                        gop.Merge(sender.ToItem.IdBanHang, sender.Item.IdBanHang);
                    }
                }
                else if (sender.mmo.MergeOption == 2)
                {
                    using (SysObj.Bh_BanHang_GopBan gop = new SysObj.Bh_BanHang_GopBan())
                    {
                        gop.MergeDetail(sender.Item.IdBanHang, sender.ToItem.IdBanHang, sender.mmo.DataSource, (sender.mmo.MenuOption == 1) ? true : false);
                    }
                    if (sender.mmo.MenuOption == 1)//merge all
                    {
                        sender.Item.IdBanHang = 0;
                        sender.Item.iState = LibExtend.Table.State.Empty;
                        mainView.lbRef_Click(null, new EventArgs());
                    }
                }
            }
        }

        private void onPrint(string v_Name, int v_IdBanHang)
        {
            DataTable DTT = new DataTable();
            using (SysObj.Bh_BanHang bh = new SysObj.Bh_BanHang())
                DTT = bh.GetDataPrint(v_IdBanHang);
            
            if (Lib.Bases.Configs.LocalXml.PrinterName.Trim() == "")
                Lib.Bases.Reports.ShowReport(v_Name, DTT, "TenQuan,DiaChi", Systems.Options.TenQuan, Systems.Options.DiaChi);
            else
                Lib.Bases.Reports.QuickPrint(v_Name, DTT, "TenQuan,DiaChi", Systems.Options.TenQuan, Systems.Options.DiaChi);
        }

        private void AssignItemFromSaleID(LibExtend.Events.Sender sender)
        {
            using (SysObj.Bh_BanHang bh = new SysObj.Bh_BanHang())
            {
                bh.IdBanHang = sender.Item.IdBanHang;
                bh.LoadData();
                sender.Item.Ngay = bh.Ngay;
                sender.Item.IdBan = bh.IdBan;
                sender.Item.ThanhTien = bh.ThanhTien;
                sender.Item.PT_CK = bh.PT_CK;
                sender.Item.Tien_CK = bh.Tien_CK;
                sender.Item.TongTien = bh.TongTien;
                sender.Item.KhachTra = bh.KhachTra;
                sender.Item.GhiChu = bh.GhiChu;
                sender.Item.DataSource = bh.GetDetail();
                using (SysObj.Bh_BanHang_GopBan mer = new SysObj.Bh_BanHang_GopBan())
                {
                    sender.Item.DataMerge = mer.LoadMergeItemByID(bh.IdBanHang);
                }
            }
        }

        private void mainView_ItemLicked(LibExtend.Table Item)
        {
            using (SysObj.Bh_BanHang bh = new SysObj.Bh_BanHang())
            {
                bh.IdBan = Item.IdBan;

                DataTable DT = bh.LoadDataByTableID();
                Item.Ngay = bh.Ngay;
                Item.IdBanHang = bh.IdBanHang;
                Item.PT_CK = bh.PT_CK;
                Item.KhachTra = bh.KhachTra;
                Item.ThanhTien = bh.ThanhTien;
                Item.GhiChu = bh.GhiChu;
                Item.TongTien = bh.TongTien;
                Item.DataSource = DT;

                using (SysObj.Bh_BanHang_GopBan mer = new SysObj.Bh_BanHang_GopBan())
                {
                    Item.DataMerge = mer.LoadMergeItemByID(bh.IdBanHang);
                }
            }
        }

        private void mainView_OnRefresh(object sender, EventArgs e)
        {
            OnLoad(false);
        }

        private void mainView_ShowBusyTable(object sender, EventArgs e)
        {
            OnLoad(Convert.ToBoolean(sender));
        }

        private void dtNgay_EditValueChanged(object sender, EventArgs e)
        {
            ReLoadHistory();
        }

        private void ReLoadHistory()
        {
            using (SysObj.Bh_BanHang bh = new SysObj.Bh_BanHang())
                grid.DataSource = bh.GetHistory(Convert.ToDateTime(dtNgay.EditValue));
        }

        private void menu1_RowDoubleClicked(object sender, EventArgs e)
        {
            DataRow row = (DataRow)sender;
            if (!object.Equals(row, null))
            {
                mainView.AddItem(row);
            }
        }

        private void xtraTabControl1_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            lblRef.Visible = (xtraTabControl1.SelectedTabPage == pgHistory) ? true : false;
        }

        private void lblRef_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ReLoadHistory();
        }
    }
}
